package org.zizi.util;

public class NameParser
{
	/** 将 source 中的字符串如“list_all” 格式化为 “listAll” */
	public static String parse(String source)
	{
		String[] a = source.toLowerCase().split("_");
		StringBuffer sb = new StringBuffer(a[0]);
		
		for (int i = 1; i < a.length; i++)
		{
			String b = a[i];
			if (b.length() == 0) continue;
			sb.append(Character.toUpperCase(b.charAt(0)));
			sb.append(b.substring(1));
		}

		return sb.toString();
	}
	
	/** 将 source 中的字符串如“listAll” 格式化为 “list_all”. */
	public static String reverse(String source)
	{
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < source.length(); i++)
		{
			char ch = source.charAt(i);
			if (ch >= 65 && ch <= 90)
			{
				if (sb.length() > 0) sb.append("_");
				sb.append(String.valueOf(ch).toLowerCase());
			}
			else
			{
				sb.append(ch);
			}
		}
		
		return sb.toString();
	}
}
